//
//  MutableCopyViewController.m
//  Pthread
//
//  Created by Yanyan Jiang on 2023/2/7.
//  Copyright © 2023 Yanyan Jiang. All rights reserved.
//

#import "MutableCopyViewController.h"
#import "CopyMutableCopyModel.h"
#import "YellowBtn.h"

@interface MutableCopyViewController ()

@end

@implementation MutableCopyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self copyAndMutablecopy];
    //[self copyAndMutablecopy2];
    //[self customCopyAndMutableCopy];
}


//非容器不可变对象与非容器可变对象的copy&mutablecopy
//测试深copy&mutablecopy
- (void)copyAndMutablecopy{
    
    NSString *str1 = @"非容器不可变对象";
    NSString *str2 = [str1 copy];
    //如果改变了地址也会改变
    //str2 = @"11223333";
    NSString *str3 = [str1 mutableCopy];
    
    //str1_p : 0x105800300, class: __NSCFConstantString
    //str2_p : 0x105800300, class: __NSCFConstantString
    //str3_p : 0x600001eb6310, class: __NSCFString
    NSLog(@"str1_p : %p, class: %@",str1,[str1 class]);
    NSLog(@"str2_p : %p, class: %@",str2,[str2 class]);
    NSLog(@"str3_p : %p, class: %@",str3,[str3 class]);
    
    
    NSMutableString *str11 = [NSMutableString stringWithFormat:@"非容器可变对象"];
    NSMutableString *str22 = [str11 copy];
    NSMutableString *str33 = [str11 mutableCopy];
    
    //str11_p :0x600001eb63d0, class: __NSCFString
    //str22_p :0x600001eb66a0, class: __NSCFString
    //str33_p :0x600001eb5f20, class: __NSCFString
    NSLog(@"str11_p :%p, class: %@",str11,[str11 class]);
    NSLog(@"str22_p :%p, class: %@",str22,[str22 class]);
    NSLog(@"str33_p :%p, class: %@",str33,[str33 class]);
}


//容器不可变对象与容器可变对象的copy&mutablecopy
- (void)copyAndMutablecopy2{
    
    NSMutableString *str11 = [NSMutableString stringWithFormat:@"非容器可变对象"];

    NSArray *array = [NSArray arrayWithObjects:@"容器不可变对象", str11,nil];
    NSArray *copyArray = [array copy];
    NSArray *mutableCopyArray = [array mutableCopy];
    
    
    //array_p: 0x600000b483c0, class: __NSSingleObjectArrayI
    //copyArray_p: 0x600000b483c0, class: __NSSingleObjectArrayI
    //mutableCopyArray_p: 0x600000719e00, class: __NSArrayM
    NSLog(@"array_p: %p, class: %@",array, [array class]);
    NSLog(@"copyArray_p: %p, class: %@",copyArray, [copyArray class]);
    NSLog(@"mutableCopyArray_p: %p, class: %@",mutableCopyArray, [mutableCopyArray class]);
    
    NSLog(@"===========原对象============");
    //object_p: 0x103396400, class: __NSCFConstantString
    //object_p: 0x600002cb5b90, class: __NSCFString
    NSLog(@"object_p: %p, class: %@",array[0],[array[0] class]);
    NSLog(@"object_p: %p, class: %@",array[1],[array[1] class]);

    NSLog(@"===========copy对象============");
    //object_p: 0x103396400, class: __NSCFConstantString
    //object_p: 0x600002cb5b90, class: __NSCFString
    NSLog(@"object_p: %p, class: %@",copyArray[0],[copyArray[0] class]);
    NSLog(@"object_p: %p, class: %@",copyArray[1],[copyArray[1] class]);

    NSLog(@"===========mutablecopy对象============");
    //object_p: 0x103396400, class: __NSCFConstantString
    //object_p: 0x600002cb5b90, class: __NSCFString
    NSLog(@"object_p: %p, class: %@",mutableCopyArray[0],[mutableCopyArray[0] class]);
    NSLog(@"object_p: %p, class: %@",mutableCopyArray[1],[mutableCopyArray[1] class]);

    //从上述输出中可以看出容器类不可变对象mutableCopy确实返回一个新的容器，但容器内的元素仍然是浅拷贝
}


//自定义copy&mutalbeCopy
- (void)customCopyAndMutableCopy{
    
    CopyMutableCopyModel *model = [[CopyMutableCopyModel alloc] init];
    CopyMutableCopyModel *copyModel = [model copy];
    CopyMutableCopyModel *mutableCopyModel = [model mutableCopy];
    
    // model_p: 0x600000550410, class:CopyMutableCopyModel
    // copyModel_p: 0x600000550400, class:CopyMutableCopyModel
    // mutableCopyModel_p: 0x600000550420, class:CopyMutableCopyModel
    NSLog(@"model_p: %p, class:%@",model,[model class]);
    NSLog(@"copyModel_p: %p, class:%@",copyModel,[copyModel class]);
    NSLog(@"mutableCopyModel_p: %p, class:%@",mutableCopyModel,[mutableCopyModel class]);
}



- (IBAction)moveBtn:(YellowBtn *)sender {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, -100, 150, 100);
    [btn setImage:[UIImage imageNamed:@"hh"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"ii"] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(clickChild:) forControlEvents:UIControlEventTouchUpInside];
    sender.accessBtn = btn;
    [sender addSubview:btn];
}

- (void)clickChild:(UIButton*)btn{
    btn.selected = !btn.selected;
}


/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
